package application;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import tools.Tool;

/**
 * Maps tool selection to a specific tool.
 *
 * Provides tool selection ability for any visual components that utilize 
 * AbstractAction's. This is especially important as it allows for the addition
 * of a new Tool without rewriting a new ToolController.
 * 
 * In summary, an AbstractAction is an object recognized by smart containers
 * such as JMenuBar's and JToolBar's.  These containers automatically register
 * the associate listners and extract any information necessary to properly
 * display the object.
 * 
 * For more information, consult the Java API for AbstractAction.
 */
@SuppressWarnings("serial")
public class ToolController extends AbstractAction
{
	// Components
	protected CanvasView canvas;
	protected Tool tool;
	protected String toolName;
	
	/**
	 * Constructor that initializes a Tool Controller
	 * 
	 * @param name Name of the tool
	 * @param tip Tip help text which appears with a mouse hover
	 * @param c The canvas for the tool
	 * @param t the tool that should be active upon selection
	 */
	public ToolController(String name, String tip, CanvasView c, Tool t)
	{
		super(name);
		toolName = name;
		tool = t;
		putValue(Action.SHORT_DESCRIPTION, tip);
		setEnabled(tool != null);
		canvas = c;
	}
	
	/**
	 * Fires whenever an action is performed, i.e. mouse click, on the controler's
	 * view, i.e container.
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e){ canvas.setcurrentTool(tool, toolName); }
}
